#include <bits/stdc++.h>
#define int long long
using namespace std;
int read() {
	int x(0),f(0);
	char ch=getchar();
	while(!isdigit(ch)) f|=(ch=='-'),ch=getchar();
	while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
	return f?-x:x;
}
const int N=100005;
int n,ans=1e18;
struct node {
	int a,b;
	bool operator < (const node y) const {
		return b<y.b;
	}
} c[N];
multiset<int> s1,s2;
multiset<int>::iterator it;
signed main() {
	freopen("boast.in","r",stdin);
	freopen("boast.out","w",stdout);
	n=read();
	for(int i=1;i<=n;++i) c[i].a=read(),c[i].b=read();
	sort(c+1,c+1+n);
	for(int i=1;i<=n;++i) s2.insert(c[i].a+c[i].b);
	for(int i=1;i<=n;++i) {
		s2.erase(s2.find(c[i].a+c[i].b));
		if(i>1) s1.insert(c[i-1].a);
		int abfi=0,abse=0,afi=0,ase=0;
		if(!s1.empty()) {
			it=s1.begin(),afi=*it;
			if(it!=(--s1.end())) ++it,ase=*it;
		}
		if(!s2.empty()) {
			it=s2.begin(),abfi=*it;
			if(it!=(--s2.end())) ++it,abse=*it;
		}
		if(ase) ans=min(ans,c[i].a+2*c[i].b+afi+ase);
		if(abse) ans=min(ans,c[i].a+abfi+abse);
		if(abfi&&afi) ans=min(ans,c[i].a+c[i].b+abfi+afi);
	}
	cout<<ans;
	return 0;
}
/*
3
1 2
2 3
3 2

11

6
1 1
4 5
1 4
1 9
1 9
8 10

15
*/
